# -*- coding: utf-8 -*-
import scrapy
import time

from JDCooker.items import JDcookerItem
from scrapy_splash import SplashRequest


class JdcookerspiderSpider(scrapy.Spider):
    name = 'JDCookerSpider'
    allowed_domains = ['jd.com']

    url = "https://list.jd.com/list.html?cat=737,752,757&page=%d&sort=sort_rank_asc&trans=1&JL=6_0_0#J_main"

    start_urls = ['https://list.jd.com/list.html?cat=737,752,757&page=1&sort=sort_rank_asc&trans=1&JL=6_0_0#J_main']

    page_no = 0

    def start_requests(self):
        for url in self.start_urls:
            yield SplashRequest(url, self.parse, args={'wait': 0.5})

    def parse(self, response):

        for each in response.xpath("//div[@class='goods-list-v2 J-goods-list gl-type-1 ']/ul/li"):
            #
            item = JDcookerItem()

            # 单价
            if len(each.xpath("./div/div[@class='p-price']/strong[@class='J_price']/i/text()").extract()) == 0:
                continue
            item['price'] = each.xpath("./div/div[@class='p-price']/strong[@class='J_price']/i/text()").extract()[0]

            # 品牌
            if len(each.xpath("./div/div[@class='p-name']/a/em/text()").extract()) == 0:
                continue
            item['brand'] = each.xpath("./div/div[@class='p-name']/a/em/text()").extract()[0]

            # # 型号
            # if len(each.xpath("./div[@class='productTitle productTitle-spu']/a/text()").extract()) == 0:
            #     continue
            # item['type'] = each.xpath("./div[@class='productTitle productTitle-spu']/a/text()").extract()[0]

            # 累计评价
            if len(each.xpath("./div/div[@class='p-commit p-commit-n']/strong/a/text()").extract()) == 0:
                continue
            item['amount'] = each.xpath("./div/div[@class='p-commit p-commit-n']/strong/a/text()").extract()[0]

            yield item

        self.page_no = self.page_no + 1

        if self.page_no < 85:
            # 每次处理完一页的数据之后，重新发送下一页页面请求
            time.sleep(1)
            yield SplashRequest(self.url % (self.page_no * 60,), self.parse, args={'wait': 0.5})
